package com.example.demospringredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class controller {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/testRedis")
    public String testString(){
        stringRedisTemplate.opsForValue().set("key","111");
        stringRedisTemplate.opsForValue().set("key2","222");
        String value=stringRedisTemplate.opsForValue().get("key");
        System.out.println(value);
        System.out.println(stringRedisTemplate.opsForValue().get("key2"));
        return "OK";

    }
    @RequestMapping("/testRedisList")
    public String testList(){

        stringRedisTemplate.execute((RedisConnection c)->{
            c.flushAll();
            return null;
        });
        stringRedisTemplate.opsForList().rightPush("key","222");
        stringRedisTemplate.opsForList().rightPush("key","333");
        System.out.println(stringRedisTemplate.opsForList().index("key",1));
        System.out.println(stringRedisTemplate.opsForList().rightPop("key",2) );
        System.out.println();
        return "OK";

    }
    @RequestMapping("/testRedisSet")
    public String testSet(){

        stringRedisTemplate.execute((RedisConnection c)->{
            c.flushAll();
            return null;
        });
        stringRedisTemplate.opsForSet().add("key","11","22","33");
        stringRedisTemplate.opsForSet().add("key1","55","33");
        System.out.println(stringRedisTemplate.opsForSet().members("key"));
        System.out.println(stringRedisTemplate.opsForSet().isMember("key", "22"));
        System.out.println(stringRedisTemplate.opsForSet().isMember("key","11","55","33"));
        System.out.println(stringRedisTemplate.opsForSet().size("key"));
        System.out.println(stringRedisTemplate.opsForSet().remove("key","11"));
        System.out.println(stringRedisTemplate.opsForSet().isMember("key","11","55","33"));
        System.out.println(stringRedisTemplate.opsForSet().difference("key","key1"));
        return "OK";

    }
    @RequestMapping("/testRedisHash")
    public String testHash(){

        stringRedisTemplate.execute((RedisConnection c)->{
            c.flushAll();
            return null;
        });
        stringRedisTemplate.opsForHash().put("key","1","2");;
        stringRedisTemplate.opsForHash().put("key","2","22");
        System.out.println(stringRedisTemplate.opsForHash().get("key", "1"));
        System.out.println(stringRedisTemplate.opsForHash().entries("key"));
        System.out.println(stringRedisTemplate.opsForHash().hasKey("key", "key3"));
        return "OK";

    }
    @RequestMapping("/testRedisZSet")
    public String testZSet(){

        stringRedisTemplate.execute((RedisConnection c)->{
            c.flushAll();
            return null;
        });
        stringRedisTemplate.opsForZSet().add("key","va1",10);
        stringRedisTemplate.opsForZSet().add("key","va2",209);
        stringRedisTemplate.opsForZSet().add("key","va3",1  );
        System.out.println(stringRedisTemplate.opsForZSet().range("key", 0, -1));
        System.out.println(stringRedisTemplate.opsForZSet().rangeWithScores("key", 0, -1));
        System.out.println(stringRedisTemplate.opsForZSet().incrementScore("key", "va3", 5));
        System.out.println(stringRedisTemplate.opsForZSet().score("key", "va3"));

        return "OK";

    }

}
